<?php


namespace app\controller\coach;


use app\BaseController;
use app\model\cart\VerifyCartModel;
use app\model\coach\UserCoachModel;
use app\model\coach\UserCoachPayModel;
use app\model\school\SchoolOrderModel;
use app\model\user\UserCouponModel;
use app\service\utils\WeiXinPayService;
use think\facade\Db;
use think\facade\Log;

class NotifyController extends BaseController
{
    /**
     * 支付回调
     * @return \Symfony\Component\HttpFoundation\Response|\think\response\Json
     */
    public function settlement()
    {
        try {
            $weiXinPayService = new WeiXinPayService();
            $app = $weiXinPayService->getApp();
            $response = $app->handlePaidNotify(function ($message, $fail) {
                Log::info('学车订单结算工资支付回调：' . json_encode($message));
                if ($message['return_code'] !== 'SUCCESS') {
                    return $fail('通信失败，请稍后再通知我');
                }
                $orderInfo = Db::name('school_order_pay')->where('order_sn', $message['out_trade_no'])->find();
                if (!$orderInfo || $orderInfo->status == 1) {
                    return true;
                }
                // 用户是否支付成功
                if ($message['result_code'] === 'FAIL') {
                    $orderInfo->status = -1;
                    $orderInfo->reason = $message['err_code_des'];
                    $orderInfo->save();
                    return true;
                }
                $orderInfo->status = 1;
                $orderInfo->save();

                // 教练已支付
                $studyOrder = SchoolOrderModel::find($orderInfo['order_id']);
                $studyOrder->status = 4;
                $studyOrder->save();
                return true;
            });
            return $response->send();
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 支付回调
     * @return \Symfony\Component\HttpFoundation\Response|\think\response\Json
     */
    public function notify()
    {
        try {
            $weiXinPayService = new WeiXinPayService();
            $app = $weiXinPayService->getApp();
            $response = $app->handlePaidNotify(function ($message, $fail) {
                Log::info('+++++++代交保证金支付回调：' . json_encode($message));
                $orderInfo = UserCoachPayModel::where('order_sn', $message['out_trade_no'])->find();
                if (!$orderInfo || $orderInfo->status == 1) {
                    return true;
                }
                if ($message['return_code'] !== 'SUCCESS') {
                    return $fail('通信失败，请稍后再通知我');
                }
                // 用户是否支付成功
                if ($message['result_code'] === 'FAIL') {
                    $orderInfo->status = -1;
                    $orderInfo->reason = $message['err_code_des'];
                    $orderInfo->save();
                    return true;
                }
                $orderInfo->status = 1;
                $orderInfo->save();
                // 教练已支付
                $userCoachInfo = UserCoachModel::find($orderInfo['user_id']);
                $userCoachInfo->is_pay = 1;
                $userCoachInfo->save();
                return true;
            });
            return $response->send();
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }
}